<?php namespace App\Models\Filters;

use EloquentFilter\ModelFilter;

class PictuerFilter extends ModelFilter
{
    /**
    * Related Models that have ModelFilters as well as the method on the ModelFilter
    * As [relationMethod => [input_key1, input_key2]].
    *
    * @var array
    */
    public $relations = [];

    public function category($url)
    {
        if ($url !== 'all') {
          return $this->whereHas('category', function ($query) use ($url) {
              $query->where('slug', $url);
          });
        }
    }
    public function type($value)
    {
      switch ($value) {
        case 'Explore':
          return $this->where('width', '>', 400)->orderBy('up_counts');
          break;
        case 'Hot':
          return $this->orderBy('view_counts');
          break;
        default:
          return $this->orderBy('created_at', 'desc');
          break;
      }
    }
    public function tags($val)
    {
        if ($val !== 'all') {
            $filter = explode(',', $val);
            return $this->whereHas('tags', function ($query) use ($filter) {
              $query->whereIn('url', $filter);
            });
        }
    }
    public function search($key)
    {
      return $this->whereLike('title', $key);
    }
    public function color($color)
    {
        if ($color !== 'all') {
            return $this->whereHas('color_average', function ($query) use ($color) {
                $query->where('basecolor', $color);
            });
        }
    }
}
